package com.ilop.sthome.ui.fragment.device;

import android.annotation.SuppressLint;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.example.common.base.BasePresentFragment;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.CustomEmptyView;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.event.EventRefreshDevice;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.mvp.contract.device.DeviceForRoomContract;
import com.ilop.sthome.mvp.present.device.DeviceForRoomPresenter;
import com.ilop.sthome.ui.activity.config.ProductActivity;
import com.ilop.sthome.ui.adapter.room.RoomAdapter;
import com.ilop.sthome.view.onScrollCallBack;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.FragmentDeviceRoomBinding;

import java.util.List;
import java.util.Objects;
import java.util.Stack;

/**
 * @author skygge
 * @date 2019-10-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备按房间分类页面
 */
public class DeviceForRoomFragment extends BasePresentFragment<DeviceForRoomPresenter, FragmentDeviceRoomBinding> implements DeviceForRoomContract.IView {

    private RoomAdapter mRoomAdapter;
    private Stack<View> mStack;
    private CustomEmptyView mEmptyView;
    private View mFootView;

    private static onScrollCallBack mCallBack;
    private boolean isFirstSyn = true;

    public static DeviceForRoomFragment newInstance(onScrollCallBack callBack){
        mCallBack = callBack;
        return new DeviceForRoomFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_device_room;
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresenter = new DeviceForRoomPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        mStack = new Stack<>();
        mRoomAdapter = new RoomAdapter(mContext);
        mDBind.rlRoomList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.rlRoomList.setAdapter(mRoomAdapter);
        ((SimpleItemAnimator) Objects.requireNonNull(mDBind.rlRoomList.getItemAnimator())).setSupportsChangeAnimations(false);
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.REFRESH_DEVICE, Integer.class).observe(this, integer -> {
            mPresenter.getRoomList();
            if (integer!=null && integer == 1 && isFirstSyn){
                mPresenter.findAllSubDevice();
                isFirstSyn = false;
            }
            if (integer!=null && integer == 2){
                removeLoadingView();
            }
        });

        LiveDataBus.get().with(CommonId.KEY_EVENT_REFRESH_DEVICE, EventRefreshDevice.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: subDevice " +  event.getRoomId());
                mPresenter.refreshByPosition(event.getDeviceName(), event.getRoomId());
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.deviceRoomScroll.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener)
                (v, scrollX, scrollY, oldScrollX, oldScrollY) -> mCallBack.onScrollDeviceType(scrollY == 0));
    }

    @Override
    public void showRoomList(List<RoomBean> room) {
        removeLoadingView();
        mRoomAdapter.setList(room);
    }

    @Override
    public void showEmptyView() {
        mRoomAdapter.setList(null);
        mDBind.roomView.setVisibility(View.GONE);
        if (mEmptyView == null) {
            mEmptyView = new CustomEmptyView(mContext);
            mDBind.frameLayout.addView(mEmptyView);
            mStack.push(mEmptyView);
            mEmptyView.setCallBack(() -> skipAnotherActivity(ProductActivity.class));
        }
    }

    @Override
    public void refreshRoom(String roomId) {
        if (mRoomAdapter.getList() == null){
            mPresenter.getRoomList();
        }else if (roomId == null){
            mRoomAdapter.notifyDataSetChanged();
        }else {
            mRoomAdapter.notifyItemRefresh(roomId);
        }
    }

    @SuppressLint("InflateParams")
    @Override
    public void showLoadingView() {
        if (mFootView == null) {
            mFootView = LayoutInflater.from(mContext).inflate(R.layout.layout_synchro_view, null, false);
            mDBind.footView.addView(mFootView);
            mStack.push(mFootView);
        }
    }

    @Override
    public void removeLoadingView() {
        if (mStack.size()>0){
            if (mEmptyView != null){
                mDBind.frameLayout.removeView(mEmptyView);
                mDBind.roomView.setVisibility(View.VISIBLE);
                mEmptyView = null;
            }
            if (mFootView != null){
                mDBind.footView.removeView(mStack.pop());
                mFootView = null;
            }
        }
    }
}
